enum DTYPEDATAS {
    //% block="天"
    DAYS,
    //% block="小时"
    HOUR,
    //% block="分钟"
    SEC,
    //% block="秒"
    MIN
}


//% color="#ef784c" iconWidth=50 iconHeight=40
namespace Unihiker_datetime{

    //% block="获取当前时间" blockType="reporter"
    export function get_now(parameter: any, block: any) {
        Generator.addImport(`import datetime`)
        Generator.addImport(`\nfrom pydate import get_now`)
        Generator.addCode(`get_now()`)
    }


    //% block="时间格式转时间戳[NAME]" blockType="reporter"
    //% NAME.shadow="string" NAME.defl="20xx/10/10"
    export function date_timestamp(parameter: any, block: any) {
        let name=parameter.NAME.code;
        Generator.addImport(`import datetime`)
        Generator.addImport(`\nfrom pydate import date_timestamp`)
        Generator.addCode(`date_timestamp(${name})`)
    }


    //% block="时间[NAME]转文本，格式为[STRFTIME]" blockType="reporter"
    //% NAME.shadow="string" NAME.defl="20xx/10/10"
    //% STRFTIME.shadow="string" STRFTIME.defl="%Y/%m/%d %H:%M:%S"
    export function date_strftime(parameter: any, block: any) {
        let name=parameter.NAME.code;
        let strftime=parameter.STRFTIME.code;
        Generator.addImport(`import datetime`)
        Generator.addImport(`\nfrom pydate import date_strftime`)
        Generator.addCode(`date_strftime(${name}, ${strftime})`)
    }


    //% block="计算时间[DATE2] - [DATE1]的结果并转换为[DTYPE]" blockType="reporter"
    //% DATE1.shadow="string" DATE1.defl="20xx/10/10"
    //% DATE2.shadow="string" DATE2.defl="20xx/10/10"
    //% DTYPE.shadow="dropdown" DTYPE.options="DTYPEDATAS" DTYPE.defl="DTYPEDATAS.a"
    export function date_timedelta(parameter: any, block: any) {
        let date1=parameter.DATE1.code;
        let date2=parameter.DATE2.code;
        let dtype=parameter.DTYPE.code;
        Generator.addImport(`import datetime`)
        Generator.addImport(`\nfrom pydate import date_timedelta`)
        Generator.addCode(`date_timedelta(${date1}, ${date2}, "${dtype}")`)
    }


    //% block="文本[NAME]转时间，格式为[STRPTIME]" blockType="reporter"
    //% NAME.shadow="string" NAME.defl="2024/10/10 17:34:45"
    //% STRPTIME.shadow="string" STRPTIME.defl="%Y/%m/%d %H:%M:%S"
    export function date_strptime(parameter: any, block: any) {
        let name=parameter.NAME.code;
        let strptime=parameter.STRPTIME.code;
        Generator.addImport(`import datetime`)
        Generator.addImport(`\nfrom pydate import date_strptime`)
        Generator.addCode(`date_strptime(${name}, ${strptime})`)
    }

}
